﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Data;
using System.Collections.Generic;
using ScrumTable.DL.Data.Schema;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{
    /// <summary>
    /// This class contains the meta data of the corresponding data
    /// object field.
    /// </summary>
    public class DomainMeta : DomainBase, IDomainMeta
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the name of the current meta object.
        /// </summary>
        public override string Name
        {
            get { return DataObject.GetData(Schema.MetaSchema.Name, string.Empty); }
            set { }
        }

        /// <summary>
        /// Gets a human readable name of the current meta object.
        /// </summary>
        public string DisplayName
        {
            get { return DataObject.GetData(Schema.MetaSchema.DisplayName, string.Empty); }
        }

        /// <summary>
        /// Gets an enumerable object which can be used to loop through the
        /// allowed values. If specified the value to set must correspond to one
        /// of these values.
        /// </summary>
        public IEnumerable AllowedValues
        {
            get { return DataObject.GetData<IEnumerable>(Schema.MetaSchema.AllowedValues, new object[0]); }
        }

        /// <summary>
        /// Gets an enumerable object which can be used to loop through the
        /// suggested values. If AllowedValues has been set you will receive
        /// the AllowedValues enumerable instance, otherwise an enumerable
        /// with the suggestions.
        /// </summary>
        public IEnumerable SuggestedValues
        {
            get { return DataObject.GetData<IEnumerable>(Schema.MetaSchema.SuggestedValues, new object[0]); }
        }

        /// <summary>
        /// Gets the default value (or initial value) of the field.
        /// </summary>
        public object DefaultValue
        {
            get { return DataObject.GetData<object>(Schema.MetaSchema.DefaultValue); }
        }

        /// <summary>
        /// Gets the back end field value type as .net runtime Type.
        /// </summary>
        public Type FieldType
        {
            get { return DataObject.GetData<Type>(Schema.MetaSchema.FieldType); }
        }

        /// <summary>
        /// Gets the default value of the field.
        /// </summary>
        public DataType FieldTypeFormat
        {
            get { return DataObject.GetData<DataType>(Schema.MetaSchema.FieldTypeFormat); }
        }

        /// <summary>
        /// Gets the string representation of the field type.
        /// </summary>
        public string FieldTypeName
        {
            get { return FieldTypeFormat.ToString(); }
        }

        /// <summary>
        /// Gets the mapped name (of the Schema) of the meta object.
        /// </summary>
        internal string MappedName
        {
            get { return DataObject.GetData(Schema.MetaSchema.MappedName, string.Empty); }
        }

        /// <summary>
        /// Gets the parent collection instance.
        /// </summary>
        internal override IDomainCollection ParentCollection
        {
            get { return Parent.SearchCollection<ScrumItemType>(); }
        }

        /// <summary>
        /// Gets a collection which contains all child collections of the current
        /// domain object.
        /// </summary>
        internal override ICollection ChildCollections { get { return new IDomainCollection[0]; } }

        /// <summary>
        /// Gets the domain object schema of the current domain object.
        /// </summary>
        protected override SchemaBase DomainSchema { get { return Schema.MetaSchema; } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Resets all child collections (IDomainCollection instances) of the
        /// current domain object.
        /// </summary>
        protected override void ResetChildCollections()
        {
        }

        /// <summary>
        /// Initializes the mappings stored in the current instance.
        /// </summary>
        protected override void InitializeMappings()
        {
            base.InitializeMappings();

            PropertySchemaMappings.Add(Schema.MetaSchema.Name, "Name");
            PropertySchemaMappings.Add(Schema.MetaSchema.MappedName, "MappedName");
            PropertySchemaMappings.Add(Schema.MetaSchema.DisplayName, "DisplayName");
            PropertySchemaMappings.Add(Schema.MetaSchema.AllowedValues, "AllowedValues");
            PropertySchemaMappings.Add(Schema.MetaSchema.DefaultValue, "DefaultValue");
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }


    /// <summary>
    /// This class contains the meta data of the corresponding data
    /// object field.
    /// </summary>
    public class DomainMeta<T> : DomainMeta
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets an enumerable object which can be used to loop through the
        /// allowed values.
        /// </summary>
        public IEnumerable<T> AllowedValuesTyped
        {
            get { return AllowedValues.Convert(o => ObjectUtil.ChangeType<T>(o)); }
        }

        /// <summary>
        /// Gets the default value of the field.
        /// </summary>
        public T DefaultValueTyped
        {
            get { return ObjectUtil.ChangeType<T>(DefaultValue); }
        }

        /// <summary>
        /// Gets an enumerable object which can be used to loop through the
        /// suggested values. If AllowedValues has been set you will receive
        /// the AllowedValues enumerable instance, otherwise an enumerable
        /// with the suggestions.
        /// </summary>
        public IEnumerable<T> SuggestedValuesTyped
        {
            get
            {
                return SuggestedValues.Convert(o => ObjectUtil.ChangeType<T>(o)); }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}